<template>
  <div>
    <h1>chpeng</h1>
    <el-form :model="searchParameters" ref="formRef">
      <el-form-item label="创建时间" prop="time1" label-width="100">
        <el-date-picker
          v-model="searchParameters.time1"
          type="daterange"
          placeholder="请选择"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="创建时间" prop="time2" label-width="100">
        <el-date-picker
          v-model="searchParameters.time2"
          type="daterange"
          placeholder="请选择"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="创建时间" prop="time3" label-width="100">
        <el-date-picker
          v-model="searchParameters.time3"
          type="daterange"
          placeholder="请选择"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        ></el-date-picker>
      </el-form-item>
    </el-form>
    <el-button @click="reset">置空</el-button>
    <el-button @click="handleSearch">搜索</el-button>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
const searchParameters = ref({
  name: "chpeng",
});
const formRef = ref(null);

// 抽离的公共方法
function processDateRanges(params) {
  const result = { ...params };
  for (const key in searchParameters.value) {
    const value = searchParameters.value[key];
    if (Array.isArray(value) && value.length === 2) {
      result[`${key}Start`] = value[0];
      result[`${key}End`] = value[1];
    }
  }
  return result;
}

function handleSearch() {
  console.log("searchParameters :>> ", searchParameters.value);
  let params = {};
  params = processDateRanges(params);
  // 这里可以添加发送请求到后台的代码
  console.log("要发送到后台的参数 :>> ", params);
}

function reset() {
  formRef.value.resetFields();
}
</script>
